package com.gscitysfy.cus.drain.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author: wangzhipeng
 * @date: 2020/7/9
 * @description: 时间工具类
 */
public class DateUtil {
    /**
     * 日期格式(yyyy-MM-dd)
     */
    public static final String yyyy_MM_dd = "yyyy-MM-dd";
    /**
     * 日期格式(yyyy-MM-dd HH:mm:ss)
     */
    public static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd";
    /**
     * 日期格式(yyyy年MM月dd日)
     */
    public static final String yyyy_MM_dd_CN = "yyyy年MM月dd日";
    /**
     * 日期格式(yyyy年MM月dd日HH时mm分ss秒)
     */
    public static final String yyyy_MM_dd_HH_mm_ss_CN = "yyyy年MM月dd日HH时mm分ss秒";
    /**
     * 日期格式(yyyy年MM月dd日HH时mm分)
     */
    public static final String yyyy_MM_dd_HH_mm_CN = "yyyy年MM月dd日HH时mm分";
    /**
     * 日期格式(HH:mm:ss)
     */
    public static final String HH_mm_ss = "HH:mm:ss";
    /**
     * 日期格式(HH:mm)
     */
    public static final String HH_mm = "HH:mm";
    /**
     * 日期格式(yyyy-MM)
     */
    public static final String yyyy_MM = "yyyy-MM";
    /**
     * 日期格式(yyyy-MM)
     */
    public static final String yyyy_MM_CN = "yyyy年MM月";
    /**
     * 日期格式(yyyyMM)
     */
    public static final String yyyyMM = "yyyyMM";

    /**
     * 日期格式(yyyy_MM_dd_HH_mm)
     */
    public static final String yyyy_MM_dd_HH_mm = "yyyyMMddHHmm";

    /**
     * DateFormat缓存
     */
    private static Map<String, DateFormat> dateFormatMap = new HashMap<String, DateFormat>();

    /**
     * 周
     */
    public static final String WEEK = "week";

    /**
     * 月
     */
    public static final String MONTH = "month";

    /**
     * 天
     */
    public static final String DAY = "day";

    /**
     * 季
     */
    public static final String SEASON = "season";

    /**
     * 年
     */
    public static final String YEAR = "year";

    /**
     * 获取周几
     *
     * @return
     */
    public static String getWeek() {
        String week = "";
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        if (weekday == 1) {
            week = "日";
        } else if (weekday == 2) {
            week = "一";
        } else if (weekday == 3) {
            week = "二";
        } else if (weekday == 4) {
            week = "三";
        } else if (weekday == 5) {
            week = "四";
        } else if (weekday == 6) {
            week = "五";
        } else if (weekday == 7) {
            week = "六";
        }
        return week;
    }

    /**
     * @param formatStr
     * @description 获取DateFormat
     */
    public static DateFormat getDateFormat(String formatStr) {
        synchronized (dateFormatMap) {
            DateFormat df = dateFormatMap.get(formatStr);
            if (df == null) {
                df = new SimpleDateFormat(formatStr);
                dateFormatMap.put(formatStr, df);
            }
            return df;
        }
    }

    public static int getQuarter() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String yearMonth = simpleDateFormat.format(new Date());
        String month = yearMonth.substring(5, 7);
        int intMonth = Integer.parseInt(month);
        int quarter = intMonth % 3 == 0 ? intMonth / 3 : intMonth / 3 + 1;
        return quarter;
    }

    public static Date getDate() {
        return Calendar.getInstance().getTime();
    }

    /**
     * 按照默认formatStr的格式，转化dateTimeStr为Date类型 dateTimeStr必须是formatStr的形式
     *
     * @param dateTimeStr
     * @param formatStr
     * @return
     */
    public static Date getDate(String dateTimeStr, String formatStr) {
        try {
            if (dateTimeStr == null || "".equals(dateTimeStr)) {
                return null;
            }
            DateFormat sdf = DateUtil.getDateFormat(formatStr);
            return sdf.parse(dateTimeStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 转化dateTimeStr为Date类型
     *
     * @param dateTimeStr
     * @return
     */
    public static Date convertDate(String dateTimeStr) {
        try {
            if (dateTimeStr == null || "".equals(dateTimeStr)) {
                return null;
            }
            DateFormat sdf = DateUtil.getDateFormat(yyyy_MM_dd_HH_mm_ss);
            Date d = sdf.parse(dateTimeStr);
            return d;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String formatDate(Date date, String formatStr) {
        DateFormat sdf = DateUtil.getDateFormat(formatStr);
        return sdf.format(date);
    }

    /**
     * 按照默认显示日期时间的格式"yyyy-MM-dd"，转化dateTimeStr为Date类型 dateTimeStr必须是"yyyy-MM-dd"的形式
     *
     * @param dateTimeStr
     * @return
     */
    public static Date getDate(String dateTimeStr) {
        return getDate(dateTimeStr, yyyy_MM_dd);
    }

    /**
     * 将YYYYMMDD转换成Date日期
     *
     * @param date
     * @return
     */
    public static Date transferDate(String date) throws Exception {
        if (date == null || date.length() < 1) {
            return null;
        }

        if (date.length() != 8) {
            throw new Exception("日期格式错误");
        }
        String con = "-";

        String yyyy = date.substring(0, 4);
        String mm = date.substring(4, 6);
        String dd = date.substring(6, 8);

        int month = Integer.parseInt(mm);
        int day = Integer.parseInt(dd);
        if (month < 1 || month > 12 || day < 1 || day > 31) {
            throw new Exception("日期格式错误");
        }

        String str = yyyy + con + mm + con + dd;
        return DateUtil.getDate(str, DateUtil.yyyy_MM_dd);
    }

    /**
     * 将Date转换成字符串“yyyy-mm-dd”的字符串
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        return dateToDateString(date, yyyy_MM_dd);
    }

    /**
     * 将Date转换成字符串“yyyy-mm-dd hh:mm:ss”的字符串
     *
     * @param date
     * @return
     */
    public static String dateToDateString(Date date) {
        return dateToDateString(date, yyyy_MM_dd_HH_mm_ss);
    }

    /**
     * 将Date转换成字符串“yyyy-mm-dd hh:mm:ss”的字符串
     *
     * @param date
     * @return
     */
    public static String dateToDateString1(Date date) {
        return dateToDateString(date, "yyyy-MM-dd HH:mm:ss");
    }


    /**
     * 将Date转换成formatStr格式的字符串
     *
     * @param date
     * @param formatStr
     * @return
     */
    public static String dateToDateString(Date date, String formatStr) {
        DateFormat df = getDateFormat(formatStr);
        return df.format(date);
    }

    /**
     * 将String转换成formatStr格式的字符串
     *
     * @param dateTime
     * @param formatStr1
     * @param formatStr2
     * @return
     */
    public static String stringToDateString(String date, String formatStr1, String formatStr2) {
        Date d = getDate(date, formatStr1);
        DateFormat df = getDateFormat(formatStr2);
        return df.format(d);
    }

    /**
     * 获取当前日期yyyy-MM-dd的形式
     *
     * @return
     */
    public static String getCurDate() {
        return dateToDateString(new Date(), yyyy_MM_dd);
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getCurDate(String formatStr) {
        return dateToDateString(new Date(), formatStr);
    }

    /**
     * 获取当前日期yyyy年MM月dd日的形式
     *
     * @return
     */
    public static String getCurCNDate() {
        return dateToDateString(new Date(), yyyy_MM_dd_CN);
    }

    /**
     * 获取当前日期时间yyyy-MM-dd HH:mm:ss的形式
     *
     * @return
     */
    public static String getCurDateTime() {
        return dateToDateString(new Date(), yyyy_MM_dd_HH_mm_ss);
    }

    /**
     * 获取当前日期时间yyyy/MM/dd HH:mm:ss的形式
     *
     * @return
     */
    public static String getCurDateTimeOracle() {
        return dateToDateString(new Date(), yyyy_MM_dd_HH_mm_ss);
    }

    /**
     * 获取当前日期时间yyyy年MM月dd日HH时mm分ss秒的形式
     *
     * @return
     */
    public static String getCurZhCNDateTime() {
        return dateToDateString(new Date(), yyyy_MM_dd_HH_mm_ss_CN);
    }

    /**
     * 比较两个"yyyy-MM-dd HH:mm:ss"格式的日期，之间相差多少毫秒,time2-time1
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long compareDateStr(String time1, String time2) {
        Date d1 = getDate(time1);
        Date d2 = getDate(time2);
        return d2.getTime() - d1.getTime();
    }

    /**
     * 比较任意格式时间相差毫秒数
     *
     * @param time1
     * @param time2
     * @param format
     * @return
     */
    public static long compareDateStr(String time1, String time2, String format) {
        Date d1 = getDate(time1, format);
        Date d2 = getDate(time2, format);
        return d2.getTime() - d1.getTime();
    }

    /**
     * 比较起始时间与当前时间相差毫秒数
     *
     * @param time
     * @param format
     * @return
     */
    public static long compareDateNow(String time, String format) {
        Date date = getDate(time, format);
        return getCurrentTimeMillis() - date.getTime();
    }

    /**
     * 比较两个"yyyy-MM-dd HH:mm:ss"格式的日期，之间相差多少毫秒,time2-time1
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long compareDateStr(Date time1, Date time2) {
        return time2.getTime() - time1.getTime();
    }

    /**
     * nows时间大于date时间 为true
     *
     * @param nows
     * @param date
     * @return
     */
    public static boolean isTimeBefor(Date nows, Date date) {
        long hous = nows.getTime() - date.getTime();
        if (hous > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 将小时数换算成返回以毫秒为单位的时间
     *
     * @param hours
     * @return
     */
    public static long getMicroSec(BigDecimal hours) {
        BigDecimal bd;
        bd = hours.multiply(new BigDecimal(3600 * 1000));
        return bd.longValue();
    }

    /**
     * 获取当前日期years年后的一个(formatStr)的字符串
     *
     * @param months
     * @param formatStr
     * @return
     */
    public static String getDateStringOfYear(int years, String formatStr) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(new Date());
        now.add(Calendar.YEAR, years);
        return dateToDateString(now.getTime(), formatStr);
    }

    /**
     * 获取当前日期mon月后的一个(formatStr)的字符串
     *
     * @param months
     * @param formatStr
     * @return
     */
    public static String getDateStringOfMon(int months, String formatStr) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(new Date());
        now.add(Calendar.MONTH, months);
        return dateToDateString(now.getTime(), formatStr);
    }

    /**
     * 获取当前日期days天后的一个(formatStr)的字符串
     *
     * @param days
     * @param formatStr
     * @return
     */
    public static String getDateStringOfDay(int days, String formatStr) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(new Date());
        now.add(Calendar.DATE, days);
        return dateToDateString(now.getTime(), formatStr);
    }

    /**
     * 获取当前日期hours小时后的一个(formatStr)的字符串
     *
     * @param hours
     * @param formatStr
     * @return
     */
    public static String getDateStringOfHour(int hours, String formatStr) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(new Date());
        now.add(Calendar.HOUR_OF_DAY, hours);
        return dateToDateString(now.getTime(), formatStr);
    }

    /**
     * 获取指定日期mon月后的一个(formatStr)的字符串
     *
     * @param date
     * @param mins
     * @param formatStr
     * @return
     */
    public static String getDateOfMon(String date, int mon, String formatStr) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(DateUtil.getDate(date, formatStr));
        now.add(Calendar.MONTH, mon);
        return dateToDateString(now.getTime(), formatStr);
    }

    /**
     * 获取指定日期day天后的一个(formatStr)的字符串
     *
     * @param date
     * @param mins
     * @param formatStr
     * @return
     */
    public static String getDateOfDay(String date, int day, String formatStr) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(DateUtil.getDate(date, formatStr));
        now.add(Calendar.DATE, day);
        return dateToDateString(now.getTime(), formatStr);
    }

    public static Date getDate(Date beginDate, int ds) {
        if (ds == 0) {
            return new Date();
        }
        try {
            SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
            Calendar date = Calendar.getInstance();
            date.setTime(beginDate);
            date.set(Calendar.DATE, date.get(Calendar.DATE) - ds);
            Date endDate = dft.parse(dft.format(date.getTime()));
            return endDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public static String getAfterNDays(Date date, int n, String formateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formateStr);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, n);
        return sdf.format(calendar.getTime());
    }

    public static Date getAfterNDays4Date(Date date, int n) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, n);
        return calendar.getTime();
    }

    /**
     * 获取指定日期mins分钟后的一个(formatStr)的字符串
     *
     * @param date
     * @param mins
     * @param formatStr
     * @return
     */
    public static String getDateOfMin(String date, int mins, String formatStr) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(DateUtil.getDate(date, formatStr));
        now.add(Calendar.SECOND, mins * 60);
        return dateToDateString(now.getTime(), formatStr);
    }

    /**
     * 获取指定日期mins分钟后的一个日期
     *
     * @param date
     * @param mins
     * @param formatStr
     * @return
     */
    public static Date getDateOfMin(Date date, int mins) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(date);
        now.add(Calendar.SECOND, mins * 60);
        return now.getTime();
    }

    /**
     * 获取当前日期mins分钟后的一个(formatStr)的字符串
     *
     * @param mins
     * @param formatStr
     * @return
     */
    public static String getDateStringOfMin(int mins, String formatStr) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(new Date());
        now.add(Calendar.MINUTE, mins);
        return dateToDateString(now.getTime(), formatStr);
    }

    /**
     * 获取当前日期mins分钟后的一个日期
     *
     * @param mins
     * @return
     */
    public static Date getDateOfMin(int mins) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(new Date());
        now.add(Calendar.MINUTE, mins);
        return now.getTime();
    }

    /**
     * 获取当前日期sec秒后的一个(formatStr)的字符串
     *
     * @param sec
     * @param formatStr
     * @return
     */
    public static String getDateStringOfSec(int sec, String formatStr) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(new Date());
        now.add(Calendar.SECOND, sec);
        return dateToDateString(now.getTime(), formatStr);
    }

    /**
     * 获得指定日期月份的天数
     *
     * @return
     */
    public static int getMonthDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getActualMaximum(Calendar.DAY_OF_MONTH);

    }

    /**
     * 获得系统当前月份的天数
     *
     * @return
     */
    public static int getCurentMonthDay() {
        Date date = Calendar.getInstance().getTime();
        return getMonthDay(date);
    }

    /**
     * 获得指定日期月份的天数 yyyy-mm-dd
     *
     * @return
     */
    public static int getMonthDay(String date) {
        Date strDate = getDate(date, yyyy_MM_dd);
        return getMonthDay(strDate);
    }

    /**
     * 获取19xx,20xx形式的年
     *
     * @param d
     * @return
     */
    public static int getYear(Date d) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        return now.get(Calendar.YEAR);
    }

    /**
     * 获取月份，1-12月
     *
     * @param d
     * @return
     */
    public static int getMonth(Date d) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        return now.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取xxxx-xx-xx的日
     *
     * @param d
     * @return
     */
    public static int getDay(Date d) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        return now.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取Date中的小时(24小时)
     *
     * @param d
     * @return
     */
    public static int getHour(Date d) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        return now.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取Date中的分钟
     *
     * @param d
     * @return
     */
    public static int getMin(Date d) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        return now.get(Calendar.MINUTE);
    }

    /**
     * 获取Date中的秒
     *
     * @param d
     * @return
     */
    public static int getSecond(Date d) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        return now.get(Calendar.SECOND);
    }

    /**
     * 得到本周周一
     *
     * @return yyyy-MM-dd
     */
    public static String getMondayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        c.add(Calendar.DATE, -day_of_week + 1);
        return dateToDateString(c.getTime(), yyyy_MM_dd);
    }

    /**
     * 得到本周周日
     *
     * @return yyyy-MM-dd
     */
    public static String getSundayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        c.add(Calendar.DATE, -day_of_week + 7);
        return dateToDateString(c.getTime());
    }

    /**
     * 得到本周周(*)
     *
     * @return yyyy-MM-dd
     */
    public static String getDayOfThisWeek(int num) {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        c.add(Calendar.DATE, -day_of_week + num);
        return dateToDateString(c.getTime(), yyyy_MM_dd);
    }

    /**
     * 得到本月指定天
     *
     * @return yyyy-MM-dd
     */
    public static String getDayOfThisMoon(String num) {
        String date = dateToDateString(new Date(), yyyy_MM);
        date = date + "-" + num;
        return date;
    }

    /**
     * 获取两个日期相差的天数
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long getQuotByDays(String beginDate, String endDate) {
        long quot = 0;
        DateFormat df = getDateFormat(yyyy_MM_dd);
        try {
            Date d1 = df.parse(beginDate);
            Date d2 = df.parse(endDate);
            quot = d2.getTime() - d1.getTime();
            quot = quot / 1000 / 60 / 60 / 24;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return quot;
    }

    /**
     * 根据日期追加的天数，得到一个新日期
     *
     * @param date
     * @param days
     * @return
     */
    public static String getDateAddDay(String date, int days, String format) {
        DateFormat df = getDateFormat(format);
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(df.parse(date));
            cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + days);

            date = df.format(cal.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    /**
     * 获取当前月的最后一天
     *
     * @return
     */
    public static Date getLastDayOfCurrMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 0);

        return cal.getTime();
    }

    /**
     * 获取指定年月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int lastDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        DateFormat df = getDateFormat(yyyy_MM_dd);
        return df.format(cal.getTime());
    }

    /**
     * 获取指定年月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        DateFormat df = getDateFormat(yyyy_MM_dd);
        return df.format(cal.getTime());
    }

    /**
     * 获取昨天日期
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getYesterday(Date date) throws ParseException {
        DateFormat df = getDateFormat(yyyy_MM_dd);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(df.parse(df.format(date)));
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return df.format(calendar.getTime());
    }

    /**
     * 10位时间戳转时间
     *
     * @param dateInt
     * @return
     */
    public static String getIntToStr(String dateInt, String format) {
        DateFormat df = getDateFormat(format);
        long times = Integer.parseInt(dateInt) * 1000L;
        Date date = new Date(times);
        return df.format(date);
    }

    /**
     * 获取 10位时间戳
     *
     * @return
     */
    public static Integer getDateInt() {
        return (int) (getCurrentTimeMillis() / 1000);
    }

    /**
     * 获得系统的时间,单位为毫秒
     *
     * @return
     */
    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    /**
     * 13位时间戳转时间
     *
     * @param time
     * @param format
     * @return
     */
    public static String getLongToStr(long time, String format) {
        Date date = new Date(time);
        return dateToDateString(date, format);
    }

    /**
     * 获取两个小时间的间隔秒杀
     *
     * @param start
     * @param end
     * @return
     */
    public static int getIntervalSec(int start, int end) {
        return (end - start) * 60 * 60;
    }

    /**
     * 毫秒时间戳毫秒加小数点
     *
     * @param time
     * @return
     */
    public static String getMillsStr(long time) {
        String timeStr = String.valueOf(time);
        String suffix = timeStr.substring(0, timeStr.length() - 3);
        String prefix = timeStr.substring(timeStr.length() - 3, timeStr.length());
        return suffix + "." + prefix;
    }

    /**
     * 带小数点的毫秒时间戳转时间格式
     *
     * @param timeStr
     * @param formatStr
     * @return
     */
    public static String longToString(String timeStr, String formatStr) {
        long times = Long.parseLong(timeStr.replace(".", ""));
        Date date = new Date(times);
        return dateToDateString(date, formatStr);
    }

    /**
     * 获取当天起始时间
     *
     * @return
     */
    public static Long getTodayTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime().getTime();
    }

    public static Integer getTodayInt() {
        return (int) (getTodayTime() / 1000);
    }

    /**
     * 获取当天结束时间
     *
     * @return
     */
    public static Long getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime().getTime();
    }

    public static Integer getTomorrowInt() {
        return (int) (getTomorrowTime() / 1000);
    }

    /**
     * 获取第二天起始时间
     *
     * @return
     */
    public static Long getTomorrowTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime().getTime();
    }

    /**
     * 获取当天指定小时的时间
     *
     * @param hour
     * @return
     */
    public static Long getPointHourTime(int hour) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, hour);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime().getTime();
    }

    /**
     * 获取当天n天后的h小时
     *
     * @param days
     * @param hour
     * @return
     */
    public static Long getPointDateHourTime(int days, int hour) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.add(Calendar.DATE, days);
        todayStart.set(Calendar.HOUR_OF_DAY, hour);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime().getTime();
    }

    /**
     * 时分秒转成秒数
     *
     * @param time
     * @return
     */
    public static Integer minTosec(String time) {
        if (time.length() <= 5) {
            time += ":00";
        }
        int index1 = time.indexOf(":");
        int index2 = time.indexOf(":", index1 + 1);
        int mi = Integer.parseInt(time.substring(0, index1));
        int ss = Integer.parseInt(time.substring(index1 + 1, index2));
        return mi * 60 + ss;
    }

    public static boolean isDate(String dateTimeStr, String formatStr) {
        DateFormat df = getDateFormat(formatStr);
        try {
            df.parse(dateTimeStr);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param date         当前时间 yyyy-MM-dd HH:mm:ss
     * @param strDateBegin 开始时间 00:00:00
     * @param strDateEnd   结束时间 00:05:00
     * @return
     */
    public static boolean isInDate(String strDate, String strDateBegin, String strDateEnd) {
        // 截取当前时间时分秒
        int strDateH = Integer.parseInt(strDate.substring(11, 13));
        int strDateM = Integer.parseInt(strDate.substring(14, 16));
        int strDateS = Integer.parseInt(strDate.substring(17, 19));
        // 截取开始时间时分秒
        int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
        int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
        int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));
        // 截取结束时间时分秒
        int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));
        int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));
        int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8));
        if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
            // 当前时间小时数在开始时间和结束时间小时数之间
            if (strDateH > strDateBeginH && strDateH < strDateEndH) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM >= strDateBeginM && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数等于开始时间分钟数，秒数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM == strDateBeginM && strDateS >= strDateBeginS && strDateS <= strDateEndS) {
                return true;
            }
            // 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数小等于结束时间分钟数
            else if (strDateH >= strDateBeginH && strDateH == strDateEndH && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数等于结束时间分钟数，秒数小等于结束时间秒数
            } else if (strDateH >= strDateBeginH && strDateH == strDateEndH && strDateM == strDateEndM && strDateS <= strDateEndS) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param date         当前时间 yyyy-MM-dd HH:mm:ss
     * @param strDateBegin 开始时间 00:00:00
     * @param strDateEnd   结束时间 00:05:00
     * @return
     */
    public static boolean isInDate(Date date, String strDateBegin, String strDateEnd) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        // 截取当前时间时分秒
        int strDateH = Integer.parseInt(strDate.substring(11, 13));
        int strDateM = Integer.parseInt(strDate.substring(14, 16));
        int strDateS = Integer.parseInt(strDate.substring(17, 19));
        // 截取开始时间时分秒
        int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
        int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
        int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));
        // 截取结束时间时分秒
        int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));
        int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));
        int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8));
        if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
            // 当前时间小时数在开始时间和结束时间小时数之间
            if (strDateH > strDateBeginH && strDateH < strDateEndH) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM >= strDateBeginM && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数等于开始时间分钟数，秒数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM == strDateBeginM && strDateS >= strDateBeginS && strDateS <= strDateEndS) {
                return true;
            }
            // 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数小等于结束时间分钟数
            else if (strDateH >= strDateBeginH && strDateH == strDateEndH && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数等于结束时间分钟数，秒数小等于结束时间秒数
            } else if (strDateH >= strDateBeginH && strDateH == strDateEndH && strDateM == strDateEndM && strDateS <= strDateEndS) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean isInTime(int time, int begin, int end) {
        if (time >= begin && time < end) {
            return true;
        }
        return false;
    }

    public static int getMinutest(String begin, String format) {
        String nowMinutes = DateUtil.getCurDate("HH:mm");
        long time = DateUtil.compareDateStr("09:00", nowMinutes, "HH:mm");
        return (int) time;
    }

    /**
     * 取得到当前时间的整点时间段
     *
     * @param
     * @return List<String>
     */
    public static List<String> getTiemSection(int step) {
        // 1分钟一次1天有24次
        int Ahoursmin = 24 / step;
        // 开始时间为0秒，第一次循环为900秒
        int AecondValueZero = 0;
        // 开始时间为-3600秒，第一次循环为0秒 ,控制步长(半个小时-1800，一个小时-3600)
        int AecondValueNineHundred = -3600 * step;
        // Map<String,String> map = new HashMap<String,String>();
        List<String> timeList = new ArrayList<String>();
        // 循环时间为00：00~23：30
        for (int i = 0; i < Ahoursmin; i++) {

            // 每次循环之前加60分钟 控制步长(半个小时+1800，一个小时+3600)
            AecondValueZero = AecondValueZero + 3600 * step;
            // 每次循环之前加60分钟 控制步长(半个小时+1800，一个小时+3600)
            AecondValueNineHundred = AecondValueNineHundred + 3600 * step;
            // 初始时间都为00：00
            String ATimeInitialFront = "00:00";
            String ATimeInitialAfter = "00:00";

            // 判断初始秒树是否为0
            if (AecondValueZero != 0) {
                // 换算成小时
                int AHourst = (Integer.valueOf(AecondValueZero) / 3600);
                // 换算成分钟
                int Aminute = (Integer.valueOf(AecondValueZero) % 3600 / 60);
                // 换算成秒
//         int Asecond = ((Integer.valueOf(AecondValueZero) % 3600 % 60 / 2));  
                // 把生成的时间转化成字符串
                String hourst = String.valueOf(AHourst);
                String minute = String.valueOf(Aminute);
                // 如果小时的长度等于1个，在其前面加个0
                if (hourst.length() == 1) {
                    hourst = "0" + hourst;
                }
                // 如果小时的到达24点让其初始化为00
                if ("24".equals(hourst)) {
                    hourst = "00";
                }
                // 如果分钟的长度等于1个，在其前面加个0
                if (minute.length() == 1) {
                    minute = "0" + minute;
                }
                // 拼接小时和分钟
                ATimeInitialAfter = hourst + ":" + minute; // + ":" + Asecond;
            }
            // 循环时间为00：30~00：00
            if (AecondValueNineHundred != 0) {
                // 换算成小时
                int AHourst = (Integer.valueOf(AecondValueNineHundred) / 3600);
                // 换算成分钟
                int Aminute = (Integer.valueOf(AecondValueNineHundred) % 3600 / 60);
                // 换算成秒
                // int Asecond = ((Integer.valueOf(AecondValueNineHundred) % 3600 % 60 / 2));
                // 把生成的时间转化成字符串
                String hourst = String.valueOf(AHourst);
                String minute = String.valueOf(Aminute);
                // 如果小时的长度等于1个，在其前面加个0
                if (hourst.length() == 1) {
                    hourst = "0" + hourst;
                }
                // 如果小时的到达24点让其初始化为00
                if (("24").equals(hourst)) {
                    hourst = "00";
                }
                // 如果分钟的长度等于1个，在其前面加个0
                if (minute.length() == 1) {
                    minute = "0" + minute;
                }
                // 拼接小时和分钟
                ATimeInitialFront = hourst + ":" + minute; // + ":" + Asecond;
            }
            // 将时间和所有字段放入map中初始为0
            Calendar now = Calendar.getInstance();
            int hour = now.get(Calendar.HOUR_OF_DAY);
            // map.put("section", ATimeInitialFront+"~"+ATimeInitialAfter);
            timeList.add(ATimeInitialFront + "~" + ATimeInitialAfter);
            if (hour == i * step + 1) {
                break;
            }
        }

        return timeList;
    }

    /**
     * 检查时间是否符合格式
     *
     * @param dateStr   时间字符串
     * @param formatStr 时间格式字符串
     * @return
     */
    public static boolean checkStringFormat(String dateStr, String formatStr) {
        if (StringUtils.isEmpty(dateStr) || StringUtils.isEmpty(formatStr)) {
            return false;
        }
        DateFormat df = getDateFormat(formatStr);
        try {
            df.setLenient(false);
            df.parse(dateStr);
        } catch (ParseException e) {
            return false;
        }
        return true;
    }

    /**
     * 检查时间是否符合格式（默认yyyy-MM-dd）
     *
     * @param dateStr
     * @return
     */
    public static boolean checkStringFormat(String dateStr) {
        return checkStringFormat(dateStr, yyyy_MM_dd);
    }

    /**
     * 日期转换对应周几
     *
     * @param datetime 日期
     * @return
     */
    public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat(yyyy_MM_dd);
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.setTime(datet);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 计算到期时间
     *
     * @param date
     * @param step
     * @return
     */
    public static Date reckonDate(Date date, int step) {
        return new Date(date.getTime() + (-step) * 24 * 60 * 60 * 1000L);
    }

    public static String getNextMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        return dateToDateString(calendar.getTime(), yyyy_MM);

    }

    public static String getThistMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return dateToDateString(calendar.getTime(), yyyy_MM);
    }

    public static String getPrecedingMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, -1);
        return dateToDateString(calendar.getTime(), yyyy_MM);
    }

    public static String getPrecedingMonthCN() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, -1);
        return dateToDateString(calendar.getTime(), yyyy_MM_CN);
    }

    public static boolean isValidDate(String str, String formatStr) {
        boolean convertSuccess = true;
        // 指定日期格式
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static Date getWorkingDay(String monthly, int num) {
        Date date = DateUtil.getDate(monthly, DateUtil.yyyy_MM);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        int i = 0;
        while (i < num) {
            int day = cal.get(Calendar.DAY_OF_WEEK);
            if (!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)) {
                i++;
            }
            cal.add(Calendar.DATE, 1);// 日期加1
        }
        return cal.getTime();
    }

    public static Date getThisMonthWorkingDay(String monthly, int num) {
        Date date = DateUtil.getDate(monthly, DateUtil.yyyy_MM);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = 0;
        while (i < num) {
            int day = cal.get(Calendar.DAY_OF_WEEK);
            if (!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)) {
                i++;
            }
            cal.add(Calendar.DATE, 1);// 日期加1
        }
        return cal.getTime();
    }

    public static Date getNextMonth(String monthly) {
        Date date = DateUtil.getDate(monthly, DateUtil.yyyy_MM);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        return cal.getTime();
    }

    public static Date getThistMonth4Date() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    public static int differentDaysByMillisecond(Date date1, Date date2) {
        return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
    }

    public static String convert(String time) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, 0);
            return sdf2.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static List<String> getpreceding12months(String monthly, int i) {
        if (StringUtils.isBlank(monthly)) {
            monthly = dateToDateString(new Date(), yyyy_MM);
        }
        Date date = DateUtil.getDate(monthly, DateUtil.yyyy_MM);
        List<String> list = new ArrayList<String>();
        while (i > 0) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, -i + 1);
            list.add(dateToDateString(cal.getTime(), yyyy_MM));
            i--;
        }
        return list;
    }

    public static List<String> get7DaysBefor() {
        List<String> dateList = new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            //calendar.set(Calendar.DAY_OF_MONTH, -i);
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + -i);
            calendar.getTime();
            dateList.add(dateToDateString(calendar.getTime(), "MM/dd"));
        }
        return dateList;
    }

    /**
     * 根据当前时间取小时和分钟
     *
     * @return dateStr
     */
    public static String getNowHour() {
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("HHmm");
        String dateStr = formatter.format(date);
        return dateStr;
    }

    public static long localDateTime2Timestamp(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static String formatLocalDate(LocalDate date, String formatStr) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formatStr);
        return dtf.format(date);
    }

    public static String formatLocalDateTime(LocalDateTime date, String formatStr) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formatStr);
        return dtf.format(date);
    }

    public static LocalDateTime long2LocalDateTime(long second) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(second), ZoneId.systemDefault());
//		return LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
    }

    /**
     * 获取指定年月的开始时间
     * <p>
     * 结果为 year-month-01 00:00:00
     *
     * @return
     */
    public static Date getBeginDate(int year, int month) {
        if (year <= 1970 || month < 1) {
            throw new IllegalArgumentException(String.format("非法参数，year: %s,month: %s", year, month));
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取下个月的开始时间
     * 例如：当前时间为 2022-04-21 15:51:20
     * 结果为 2022-05-01 00:00:00
     *
     * @return
     */
    public static Date getNextMonthBeginDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 将字符串格式（HH:mm:ss）时间转换为 LocalDateTime类型
     *
     * @param dateString
     * @return
     */
    public static LocalDateTime parseLocalDateTimeByDateString(String dateString) {
        LocalDateTime localDateTime = null;
        try {
            localDateTime =
                    new SimpleDateFormat(HH_mm_ss)
                            .parse(dateString)
                            .toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return localDateTime;
    }

    /**
     * 将字符串格式（HH:mm）时间转换为 LocalDateTime类型
     *
     * @param dateString
     * @return
     */
    public static LocalDateTime parseLocalDateTimeByHourMinute(String dateString) {
        LocalDateTime localDateTime = null;
        try {
            localDateTime =
                    new SimpleDateFormat(HH_mm)
                            .parse(dateString)
                            .toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return localDateTime;
    }

    public static LocalDateTime parseLocalDateTime(String str) {
        if (str == null) {
            return null;
        }
        LocalDateTime localDateTime;
        try {
            if (str.contains("/")) {
                localDateTime = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
            } else {
                localDateTime = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
            return localDateTime;
        } catch (Exception e) {
            return null;
        }
    }


    public static Date getLastMonth(Date planDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(planDate);
        calendar.add(Calendar.MONTH, -1);
        return calendar.getTime();
    }

    public static Date getMonthBeginDate(Date planDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(planDate);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getMonthEndDate(Date planDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(planDate);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 当前时间加几个月
     *
     * @param month
     * @return
     */
    public static Date addMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }

    /**
     * 当前时间加几天
     *
     * @param day
     * @return
     */
    public static Date addDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    /**
     * 日期转10位时间戳
     *
     * @param date
     * @return
     */
    public static Integer DateToInt(Date date) {
        return (int) (date.getTime() / 1000);
    }

    /**
     * 10位时间戳转
     *
     * @param time
     * @return
     */
    public static Date intToDate(int time) {
        return new Date(time * 1000L);
    }


    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 获取下周一时间
     *
     * @param date
     * @return
     */
    public static Date getNextWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, 7);
        return cal.getTime();
    }


    /**
     * 获取下个月一号时间
     */
    public static Date getPerFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 获取下一年一月一号时间
     */
    public static Date getPerFirstDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMinimum(Calendar.DAY_OF_YEAR));
        return calendar.getTime();
    }


    public static Long getTimeForS(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(dateStr);
        long time = date.getTime() / 1000l;
        return time;
    }

    public static void main(String[] args) throws ParseException {
        String[] times = {"2022-10-27 21:50:08", "2022-10-27 21:51:08",
                "2022-10-27 21:52:08", "2022-10-27 21:53:08",
                "2022-10-27 21:54:08", "2022-10-27 21:55:08",
                "2022-10-27 21:56:08", "2022-10-27 21:57:08"};
        long timeLongJ = 0l;
        long timeLongO = 0l;

        for (int i = 0; i < times.length; i++) {
            if (i % 2 == 1) {//为奇数
                timeLongJ = getTimeForS(times[i]) + timeLongJ;
            } else {
                timeLongO = getTimeForS(times[i]) + timeLongO;
            }
        }
        long result = timeLongJ - timeLongO;
        System.out.println("相减结果为：" + result);

    }

    /**
     * 获取两个日期之间的所有月份 (年月)
     *
     * @param startTime
     * @param endTime
     * @return：list
     */
    public static List<String> getMonthBetweenDate(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        // 声明保存日期集合
        List<String> list = new ArrayList<>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {

                // 把日期添加到集合
                list.add(sdf.format(startDate));

                // 设置日期
                calendar.setTime(startDate);

                //把月数增加 1
                calendar.add(Calendar.MONTH, 1);

                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取年的所有月份 (年月)
     *
     * @return：list
     */

    public static List<String> getMonthByYear(String year) {

        // 声明保存日期集合
        List<String> list = new ArrayList<>();
        String[] array = new String[12];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR,Integer.parseInt(year));
        cal.set(Calendar.MONTH, 0);
        for(int i=0;i<12;i++){
            list.add(sdf.format(cal.getTime()));
            cal.add(Calendar.MONTH, 1);
        }
        return list;
    }

    /**
     * 获取两个日期之间的所有年
     *
     * @param startTime
     * @param endTime
     * @return：YYYY-MM
     */
    public static List<String> getYearBetweenDate(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.YEAR, 1);
                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取两个日期之间的所有季度
     *
     * @param startTime
     * @param endTime
     * @return：YYYY-MM
     */
    public static List<String> getQuarterBetweenDate(String startTime, String endTime) {

        List<String> list = getYearBetweenDate(startTime, endTime);
        List<String> quarter = new ArrayList<String>();

        for (String time : list) {
            String one = time + "-01";
            String two = time + "-04";
            String three = time + "-07";
            String four = time + "-10";
            quarter.add(one);
            quarter.add(two);
            quarter.add(three);
            quarter.add(four);
        }
        return quarter;
    }
    /**
     * 获取两个日期之间的所有季度
     *
     * @param startTime
     * @param endTime
     * @return：YYYY-MM
     */
    public static List<String> getQuarterBetweenDateZQ(String startTime, String endTime) {

        List<String> list = getYearBetweenDate(startTime, endTime);
        List<String> quarter = new ArrayList<String>();

        for (String time : list) {
            String one = time + "一季度";
            String two = time + "二季度";
            String three = time + "三季度";
            String four = time + "四季度";
            quarter.add(one);
            quarter.add(two);
            quarter.add(three);
            quarter.add(four);
        }
        return quarter;
    }
    /**
     * 获取两个日期之间的所有汛期
     *
     * @param startTime
     * @param endTime
     * @return：YYYY-MM
     */
    public static List<String> getXunQiBetweenDate(String startTime, String endTime) {

        List<String> list = getYearBetweenDate(startTime, endTime);
        List<String> xunQi = new ArrayList<String>();

        for (String time : list) {
            String one = time + "-07-01 00:00:00";
            String two = time + "-08-01 00:00:00";
            String three = time + "-09-01 00:00:00";
            xunQi.add(one);
            xunQi.add(two);
            xunQi.add(three);
        }
        return xunQi;
    }
}
